package com.mioto.ld.module.statistics;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.mioto.ld.module.statistics.model.SameCompareStatisticsVO;
import com.mioto.ld.module.statistics.model.SearchConditionDTO;

import java.util.Date;


/**
 * @author mioto-qinxj
 * @date 2024/6/16
 * @desc
 */
public class YearStatistics extends AbstractStatistics {

    private String formatStr = "yyyy-MM";
    public YearStatistics() {
        index = 12;
    }

    @Override
    protected void calSearchDate(SearchConditionDTO searchDTO) {
        searchDTO.setEndTime(DateUtil.dateSecond());
        Date temp = DateUtil.offsetMonth(searchDTO.getEndTime(),-index + 1);
        searchDTO.setStartTime(DateUtil.parse(DateUtil.format(temp,formatStr),formatStr));
        searchDTO.setSameCompareStartDate(DateUtil.offsetMonth(searchDTO.getStartTime(), -index));
        searchDTO.setSameCompareEndDate(DateUtil.offsetSecond(searchDTO.getStartTime(),-1));
    }

    @Override
    protected SameCompareStatisticsVO[] buildTempArray(Date startDate, int type) {
        SameCompareStatisticsVO[] array = new SameCompareStatisticsVO[index];
        DateTime dateTime;
        for (int i = 0; i < index; i++) {
            dateTime = DateUtil.offsetMonth(startDate,i);
            array[i] = new SameCompareStatisticsVO();
            array[i].setTime(buildTimeStr(startDate,i));
            array[i].setType(type);
            array[i].setFormatDate(DateUtil.format(dateTime,  formatStr));
        }
        return array;
    }

    @Override
    protected String buildTimeStr(Date startDate, int index) {
        DateTime dateTime = DateUtil.offsetMonth(startDate,index);
        int num = dateTime.month() + 1;
        if (num < 10){
            return "0" + num;
        }
        return num + "";
    }
}
